<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, i) in goodsList" :key="i" @click="goToGoodsDetail(goods)" >
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        params: {
          query: '', //关键字
          cid: '', //分类id
          pagesize: 10,
          pagenum: 1 //页数
        },
        total: 0, //总条数
        goodsList: [],
        // 是否正在请求数据
        isloading: false
      };
    },
    onLoad() {
      this.getGoodsList()
    },
    //触底事件
    // 触底的事件
    onReachBottom() {
      // 判断是否还有下一页数据
        if (this.params.pagenum * this.params.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
      //节流阀没开没有加载完成不能再次进行触底事件
      if(this.isloading) return
      // 让页码值自增 +1
      this.params.pagenum += 1
      // 重新获取列表数据
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.goodsList = []
      this.params.pagenum = 1
      this.getGoodsList(uni.stopPullDownRefresh)
    },
    methods: {
      //获取商品列表数据
      async getGoodsList() {
        //打开节流阀
        this.isloading = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.params)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
        //关闭节流阀
        this.isloading = false
      },
      //点击跳转
      goToGoodsDetail(a){
        const id = a.goods_id
        uni.navigateTo({
          url:'../goods_detail/goods_detail?goods_id=' + id
        })
      }
    },

  }
</script>

<style lang="scss">

</style>
